import { _decorator, Collider2D, Component, Node, PhysicsSystem2D, Vec2, Vec3 } from 'cc';
import { Bullet } from './Bullet';
import { Laser } from '../Shader/Laser';
import { Unit } from './Unit';
import { UnitType } from '../Util/Global';
import { DestructableCom } from './Component/DestructableCom';
import { EconomicCom } from './Component/EconomicCom';
const { ccclass, property } = _decorator;

@ccclass('BulletLaser')
export class BulletLaser extends Bullet {
    @property(Laser)
    public laser_com: Laser = null;

    @property({ displayName: '伤害间隔' })
    public trigger_interval: number = 0.1;
    @property({ override: true, displayName: '每次跳伤害' })
    public atk: number = 1;

    public _lv: number = 1;
    set lv(va) {
        this._lv = va;
        this._atk = this.atk * va;
    }

    get lv() {
        return this._lv;
    }

    protected laser_end_dur = 1;
    protected hit_target: Unit = null;
    start(): void {
        super.start();
    }

    SetTarget(target: Unit): void {
        this.target = target;

        this.laser_com.collisionConditionF = this.onCheckHitCond.bind(this);
        this.laser_com.setHitEventCallback(this.onLaserHitHanlder.bind(this));
    }

    SetLaserFireDir(dir: Vec2 | Vec3, wpos?: Vec3) {
        this._laser_dir.set(dir.x, dir.y);
        this._laser_pos = wpos;
        // this.laser_com.fire(wpos, this._laser_dir);
    }

    protected onDisable(): void {
        super.onDisable();
        // this._laser_dir.set(0, 0);
        this._laser_pos = null;
        this._laser_end = false;
        this._lv = 1;
    }

    private _tirgger_cool_time = 0;
    private _laser_dir: Vec3 = new Vec3;
    private _laser_pos: Vec3 = null;
    private _laser_end: boolean = false;
    protected updateLogic(dt: number): void {
        if (this._lifeTime >= this.canLifeTime + this.laser_end_dur) {

            this.DestroySelf();
        }
        else if (!this._laser_end && this._lifeTime > this.canLifeTime) {
            this._laser_end = true;
            this.laser_com.LaserEndAimi(this.laser_end_dur);

        }

        if (this._tirgger_cool_time > 0) {
            this._tirgger_cool_time -= dt;
        }
        else {
            this._tirgger_cool_time = this.trigger_interval;
            if (this.hit_target) {

                let destructable = this.hit_target.getComponent(DestructableCom);
                if (destructable) {
                    destructable.OnDamage(this);
                }
                else {
                    let ec = this.hit_target.getComponent(EconomicCom);
                    if (ec) {
                        ec.OnHitDamage(this);
                    }
                }
            }
        }

        let wpos: Vec3 = this._laser_pos;

        //如果没设定laser的位置，就默认在父级位置
        if (wpos == null && this.parentUnit && !this.parentUnit.death) {
            wpos = this.parentUnit.unitWorldPos;
            if (this._laser_dir) {//偏移
                wpos.add(this._laser_dir.clone().normalize().multiplyScalar(50))
            }
        }

        if (this.target && !this.target.death) {
            this._laser_dir = this.target.unitWorldPos.subtract(this.node.worldPosition);
        }
        else {
            this.laser_com.end_hit = null;
        }
        this.laser_com.fire(wpos, this._laser_dir);//持续瞄准目标

        this.laser_com.updateLaser(PhysicsSystem2D.instance.fixedTimeStep);
    }

    /**最近的敌对碰撞事件处理 */
    protected onLaserHitHanlder(co: Collider2D) {
        if (co && this._tirgger_cool_time <= 0) {//触发伤害
            // console.log('命中!');
            let u = co.getComponent(Unit);
            if (u) {
                this.hit_target = u;
            }
        }
    }

    /**过滤出敌对对象 */
    protected onCheckHitCond(co: Collider2D) {
        if (co) {
            let unit = co.getComponent(Unit);
            if (this.IsEnemy(unit)) {
                return true;
            }
        }

        return false;
    }
    IsEnemy(unit: Unit): boolean {
        if (!unit || !unit.node.active ||
            !(unit.unitType > UnitType.UnitSpacecra)) {
            return false;
        }
        if (unit
            && unit.parentUnit != this.parentUnit
            && unit != this.parentUnit) {
            return true;
        }
        return false;
    }
}

